package com.steven.ourcontest.information.view.admin

import android.os.Bundle
import android.view.View
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.steven.ourcontest.R
import com.steven.ourcontest.base.BaseActivity
import com.steven.ourcontest.information.adapter.AdminManageAdapter
import com.steven.ourcontest.information.viewmodel.AdminManageViewModel
import com.steven.ourcontest.information.widget.ConfirmCancelDialog
import com.steven.ourcontest.utils.toast
import kotlinx.android.synthetic.main.activity_admin_manage.*

class AdminManageActivity : BaseActivity() {
    private val adminManageViewModel: AdminManageViewModel by viewModels()
    private val adapter by lazy {
        AdminManageAdapter(this, {
            confirmCancelDialog.setConfirmClick {
                adminManageViewModel.updateIdentifyStatus(it, true)
            }.setContent("确认通过？").show()
        }, {
            confirmCancelDialog.setConfirmClick {
                adminManageViewModel.updateIdentifyStatus(it, false)
            }.setContent("确认拒绝？").show()
        })
    }
    private val confirmCancelDialog by lazy {
        ConfirmCancelDialog(this)
    }
    private val snapHelper: PagerSnapHelper by lazy {
        object : PagerSnapHelper() {
            override fun findTargetSnapPosition(
                layoutManager: RecyclerView.LayoutManager,
                velocityX: Int,
                velocityY: Int
            ): Int {
                return super.findTargetSnapPosition(layoutManager, velocityX, velocityY)
            }

            override fun findSnapView(layoutManager: RecyclerView.LayoutManager): View? {
                return super.findSnapView(layoutManager)
            }
        }
    }

    override fun getStatusBarColor(): Int {
        return R.color.white
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initListener()
    }

    override fun getViewId(): Int? {
        return R.layout.activity_admin_manage
    }

    override fun initView() {
        recycler_container?.adapter = adapter
        snapHelper.attachToRecyclerView(recycler_container)
        recycler_container?.viewTreeObserver?.addOnGlobalLayoutListener(object :
            OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                recycler_container?.viewTreeObserver?.removeOnGlobalLayoutListener(this)
            }
        });
    }

    private fun initListener() {
        adminManageViewModel.usersList.observe(this, Observer {
            if (it?.msg == "success") {
                adapter.itemData.clear()
                adapter.itemData.addAll(it.data)
                adapter.notifyDataSetChanged()
            } else {
                toast(it?.msg.toString())
            }
        })
        adminManageViewModel.updateRes.observe(this, Observer {
            if (it?.msg == "success") {
                if (confirmCancelDialog.isShowing) {
                    confirmCancelDialog.dismiss()
                }
                adminManageViewModel.queryToBeConfirm()
            } else {
                toast(it?.msg.toString())
            }
        })
    }

    override fun onResume() {
        super.onResume()
        adminManageViewModel.queryToBeConfirm()
    }
}